import pygtk
pygtk.require('2.0')
import gtk
import sound
import os
import time

TYPING_ROOT = os.path.dirname(os.path.abspath(__file__))
while(os.path.isfile(TYPING_ROOT)):
    TYPING_ROOT = os.path.dirname(TYPING_ROOT)
IMAGE_SOURCE = TYPING_ROOT + os.sep + "images" + os.sep + "keyboard" + os.sep + "keypress"

KEY_X = 600
KEY_Y = 250

class Typing(gtk.EventBox):

    def __init__(self, EBox):#, spaceFunction):
        super(Typing, self).__init__()

        #Create a table to hold the keyboard in
        #self.table = gtk.Table(4, 10, True)
        #self.table.set_flags(gtk.CAN_DEFAULT)
        #self.table.set_row_spacings(20)
        #self.table.set_col_spacings(20)
        
        #self.add(self.table)
        
        #self.backbutton = gtk.Button()
        #image = gtk.Image()
        #image.set_from_file(IMAGE_SOURCE + os.sep + "backbutton.gif")
        #image.show()
        #self.backbutton.set_image(image)
        #self.backbutton.show() 
        #self.table.attach(self.backbutton, 9, 10, 0, 1)
        #self.back = self.backbutton.connect("clicked", spaceFunction)       
        
        #EBox.connect("key-press-event", self.keypresscall)       
        #EBox.connect("key-release-event", self.keyreleasecall)       
        
        self.dict_pixbuf = {}
        alphastring = "abcdefghijklmnopqrstuvwxyz"
        
        for i in range(len(alphastring)):
            letter = alphastring[i]
            self.dict_pixbuf[letter] = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + letter + ".gif").scale_simple(KEY_X, KEY_Y, gtk.gdk.INTERP_BILINEAR)
        
        self.dict_pixbuf[" "] = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + "space.gif").scale_simple(KEY_X, KEY_Y, gtk.gdk.INTERP_BILINEAR)
        self.keyispressed = False
        self.lastpressed = ""
        
        # Need to make this adjustable, not hard coded scaling?
        self.keyboardimage = gtk.TextView()
        self.keyboardimage.set_cursor_visible(False)
        self.keyboardimage.set_justification(gtk.JUSTIFY_CENTER)
        self.keyboardimage.set_editable(False)
        
        basepixbuf = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + "hands.gif").scale_simple(KEY_X, KEY_Y, gtk.gdk.INTERP_BILINEAR)
        self.dict_pixbuf["base"] = basepixbuf
        self.highlightkey("base")
        
        #self.keyboardimage.set_from_pixbuf(self.dict_pixbuf["a"])
        self.keyboardimage.show()
        
        self.keyboardframe = gtk.Frame()
        self.keyboardframe.add(self.keyboardimage)
        self.keyboardframe.show()
        self.add(self.keyboardframe)
        
        #self.table.attach(self.keyboardframe, 0, 10, 0, 4)
        #self.table.show()
        
        
    def highlightkey(self, letter):
        if (self.dict_pixbuf.has_key(letter)):
            buffer = self.keyboardimage.get_buffer()
            buffer.set_text("\n\n")
            buffer.insert_pixbuf(buffer.get_end_iter(), self.dict_pixbuf[letter])
            self.keyboardimage.show()
            
    def keypresscall(self, widget, event):
        print "hearing keypress call %d" %event.keyval
        
        #if (self.keyispressed == False):
        #    self.highlightkey("base")
        #    while gtk.events_pending():
        #        gtk.main_iteration(False)
        #    time.sleep(.005)

        #self.keyispressed = True
        
        if (event.keyval <= 122 and event.keyval >= 97):
            letter = event.string
            if (letter != self.lastpressed):
                self.highlightkey(letter)
                self.lastpressed = letter
        elif (event.keyval == 32):
            self.highlightkey(" ")

    
    def keyreleasecall(self, widget, event):
        keyispressed = False
        #self.highlightkey("base")

def main():
    #gtk.main()
    return 0       

if __name__ == "__main__":
    #Typing()
    main()
